﻿#region File Info Header
/*________________________________________________________________________________________

  Copyright (C) 2011 Jason Zhang, eagleboost@msn.com

  * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
  * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

________________________________________________________________________________________*/
#endregion File Info Header

namespace EagleBoost.Wpf.Presentation.Converters
{
  using System;
  using System.Globalization;
  using System.Windows.Data;
  using Infra.Extensions;

  /// <summary>
  /// Convert a numeric value to its opposite value
  /// </summary>
  [ValueConversion(typeof(int), typeof(int))]
  [ValueConversion(typeof(double), typeof(double))]
  [ValueConversion(typeof(decimal), typeof(decimal))]
  public class NegativeConverter : ConverterMarkupExtension<NegativeConverter>, IValueConverter
  {
    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return ToNegative(value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return ToNegative(value);
    }
    #endregion IValueConverter Members

    #region Statics
    public static object ToNegative(object value)
    {
      if (!value.GetType().IsNumber())
      {
        throw new ArgumentException(string.Format("{0} is not a number", value));
      }

      if (value is int)
      {
        return -(int)value;
      }

      if (value is double)
      {
        return -(double)value;
      }

      return -(decimal)value;
    }
    #endregion Statics
  }
}
